package internal

import (
	"github.com/gin-gonic/gin"
	"lol.com/feb214-game/nest.git/tools/jsonutils"
	"net/http"
	"sync/atomic"
	"talent.com/cube/common/cache"
	"talent.com/cube/common/cxt"
	"talent.com/cube/conf"
	"talent.com/cube/conf/g"
)

func getServerInfo(c *gin.Context) {
	allCount := make(map[string]int32)
	var userCount int32
	var tc *cxt.TableContext
	g.AllTables.Range(func(key, value interface{}) bool {
		tc = value.(*cxt.TableContext)
		allCount[g.GetCodeLabel(tc.GameType)]++
		userCount += atomic.LoadInt32(&tc.UserCount)
		//不能直接用len(tc.Players)算机器人，有并发map读写问题
		return true
	})
	allCount["user"] = userCount
	waterPool := make(map[string]int64)
	for _, label := range conf.ServerConfig().Server.EnabledGame {
		code := g.GetLabelCode(label)
		bet, _ := cache.GetDailyBet(code)
		award, _ := cache.GetDailyAward(code)
		waterPool[label] = bet - award
	}
	c.JSON(http.StatusOK, jsonutils.JsonObject{
		"count": allCount,
		"pool":  waterPool,
	})
}
